﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using car.Models;

namespace car.Controllers
{
    public class UserController : Controller
    {
        public IActionResult Index()
        {
            ViewData["Flag"] = "none";
            try
            {
                using (var db = new carContext())
                {
                    return View(db.User.ToList());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }
        public IActionResult Create()
        {
            return View();
        }
        public IActionResult Created()
        {
            User user = new User();
            string id = Request.Form["id"];
            string uid = Request.Form["uid"];
            string sex = Request.Form["sex"];
            string upwd = Request.Form["pwd"];
            int age = int.Parse(Request.Form["age"]);
            string name = Request.Form["name"];
            int tel = int.Parse(Request.Form["tel"]);
            user.Id = id;
            try
            {
                using (var db = new carContext())
                {
                    user=db.User.Find(id);
                    if (user == null)
                    {
                        user = new User();
                        user.Id = id;
                    }
                    user.Uid = uid;
                    user.Name = name;
                    user.Tel = tel;
                    user.Age = age;
                    user.Sex = sex;
                    user.Upwd = upwd;
                    db.User.Add(user);
                    db.SaveChanges(true);
                    ViewData["Flag"] = "true";
                }
            }
            catch(Exception)
            {
                throw;
            }
            return View("Create");
        }
        
        public IActionResult Delete(string id)
        {
            User user=new User();
            //string id = Request.Form["id"];
            user.Id = id;
            try
            {
               
                using(var db = new carContext())
                {
                    user=db.User.Find(id);
                    if(user!=null)
                    {
                        db.User.Remove(user);
                        ViewData["Flag"] = true;
                    }
                    else
                    {
                        ViewData["Flag"] = false;
                    }
                    db.SaveChanges();
                    return View("Index",db.User.ToList());
                }    
            }
            catch
            {
                
                ViewData["Flag"] = false;
            }
            return View("Index");
        }

        public IActionResult Update(string id)
        {
            ViewData.Clear();
            ViewData["Uid"] = id;
            return View();
        }
        public IActionResult UserUpdate(string id)
        {
            ViewData.Clear();
            ViewData["Uid"] = id;
            return View();
        }
        public IActionResult Updated()
        {
            User user;
            string id= Request.Form["id"];
            string uid=Request.Form["uid"];
            string sex = Request.Form["sex"];
            string upwd = Request.Form["pwd"];
            int age = int.Parse(Request.Form["age"]);
            string name = Request.Form["name"];
            int tel = int.Parse(Request.Form["tel"]);
            try
            {
                using (var db=new carContext())
                {
                    user=db.User.Find(uid);
                    if(user==null)
                    {
                        user= new User();
                        user.Uid=uid;
                    }
                        user.Id = id;
                        user.Name = name;
                        user.Tel = tel;
                        user.Age = age;
                        user.Sex = sex;
                        user.Upwd = upwd;
                        db.User.Update(user);
                    db.SaveChanges(true);
                    ViewData["Flag"]="true";
                }
                
            }
            catch(Exception ex)
            {
                ViewData["Flag"]="false";
                throw ex;
                
            }
            if (HomeController.UserUid == null)
            {
                return View("Update");
            }
            else return View("UserUpdate");
                
        }
            public IActionResult UserIndex()
            {
                ViewData["Flag"] = "none";
                try
                {
                    List<User> users = new List<User>();
                    using (var db = new carContext())
                    {
                        var user = db.User.Find(HomeController.UserUid);
                        users.Add(user);
                        return View(users);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
        [HttpPost]
        public IActionResult SearchID(string name)
        {
            var subscribelist = new carContext();
            var filelist = subscribelist.User.ToList();
            if (!string.IsNullOrEmpty(name))
                filelist = filelist.Where(p => p.Uid.Contains(name)).ToList();
            return View("Index", filelist);
        }
        /*[HttpPost]
        public IActionResult SearchName(string name)
        {
            var subscribelist = new Press_management();
            var filelist = subscribelist.Press.ToList();
            if (!string.IsNullOrEmpty(name))
                filelist = filelist.Where(p => p.PressName.Contains(name)).ToList();
            return View("List", filelist);
        }*/
    }  
}
